package com.sqsf.javabean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

//4个注解功能相同就是用于区分作用
@Controller("u")//等价于<bean id="u" class="com.sqsf.javabean.User"></bean>
public class User {
    @Value("2")
    private int id;
    @Value("李四")
    private String name;
    @Value("27")
    private int age;
    //对于自定义类型而言需要现将对应的对象交由spring管理
    //自动查找spring容器中类型相同的对象存入
    @Autowired//等价与bean标签属性配置autowire="byType"
    @Qualifier("r")//如果类型匹配失败 使用指定的id进行匹配

    //java也提供了可以注入的注解
    //@Resource//先根据属性名查找 再根据类型查找
    private Role r;

    public User() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Role getR() {
        return r;
    }

    public void setR(Role r) {
        this.r = r;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", r=" + r +
                '}';
    }
    //生命周期注解 对于单例对象 spring容器创建时创建 关闭时自动调用销毁方法
    @PostConstruct//等价于bean标签属性init-method="a"
    public void a(){
        System.out.println("初始化");
    }
    @PreDestroy//等价于bean标签属性destroy-method="b"
    public void b(){
        System.out.println("销毁");
    }
}
